package com.AAL.DAO;

import java.sql.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.ISourcingRequestDAO;
import com.AAL.Model.Employee;
import com.AAL.Model.SourcingRequest;
import com.AAL.Model.StatusOfRequest;
import com.AAL.Utils.HibernateUtil;

/*
 * Loc DV
 * Create date:30.3.2011
 */
public class SourcingRequestDAO implements ISourcingRequestDAO {

	@Override
	public SourcingRequest saveAndUpdateSourcingRequest(
			SourcingRequest sourcingRequest) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SourcingRequest deleteSourcingRequest(String sourcingRequestID) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SourcingRequest getSourcingRequest(String sourcingRequestID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		SourcingRequest sourcingRequest = (SourcingRequest) session.load(
				SourcingRequest.class, sourcingRequestID);
		return sourcingRequest;
	}

	@Override
	public List<SourcingRequest> getLstSourcingRequestsByEmployee(
			Employee employee, Date fromDate, Date toDate,
			StatusOfRequest statusOfRequest) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SourcingRequest> getLstSourcingRequests(Date fromDate,
			Date toDate, StatusOfRequest statusOfRequest) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SourcingRequest> getLstSourcingRequests() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		List<SourcingRequest> sourcingRequest = null;
		try {
			session.beginTransaction();

			sourcingRequest = (List<SourcingRequest>) session.createQuery(
					"from SourcingRequest").list();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}

		return sourcingRequest;

	}
	//phat
	@SuppressWarnings("unchecked")
	@Override
	public List<SourcingRequest> getLstSourcingRequestsAcceptByWareHouse(
			String regionID) {
		// TODO Auto-generated method stub
		System.out.println("goi getLstSourcingRequestsByWareHouse:"+regionID);
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		List<SourcingRequest> sourcingRequest = null;
		try {
			session.beginTransaction();

			sourcingRequest = (List<SourcingRequest>) session.createQuery(
					"from SourcingRequest as SR where SR.employeeByHO.account.accountInformation.city.region.regionID='"+regionID+"' and SR.statusOfRequest.statusOfRequestID=3").list();
			session.getTransaction().commit();
			System.out.println("size: "+sourcingRequest.size());
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}

		return sourcingRequest;
	}
	//phat end

}
